package com.cupcorn.balapkarung.entity;

import java.util.ArrayList;

import org.andengine.extension.tmx.TMXLayer;
import org.andengine.extension.tmx.TMXObject;
import org.andengine.extension.tmx.TMXObjectGroup;
import org.andengine.extension.tmx.TMXTiledMap;
import org.andengine.util.color.Color;

import com.cupcorn.balapkarung.GameDefine;
import com.cupcorn.lib.Anchor;
import com.cupcorn.lib.Ratio;
import com.cupcorn.lib.define.GameEngineConfiguration;
import com.cupcorn.lib.display.DisplayRectangle;
import com.cupcorn.lib.display.DisplayTmx;
import com.cupcorn.lib.game.GameEngine;
import com.cupcorn.lib.game.GameRectangle;
import com.cupcorn.lib.game.element.GameElementRectangle;

public class Map implements GameDefine
{
	final int						SIZE_GROUND			= 32;
	int								xCount, yCount, xCountMove, xMove;
	float							alpha;
	
	GameEngine						engine;
	TMXTiledMap						map;
	TMXLayer[]						tmxLayer;
	MC								mc;
	
	ArrayList<GameElementRectangle>	listElementGround	= new ArrayList<GameElementRectangle>();
	DisplayRectangle				displayGround;
	public GameRectangle[]			ground;
	
	ArrayList<GameElementRectangle>	listElementGameOver	= new ArrayList<GameElementRectangle>();
	DisplayRectangle				displayGameOver;
	public GameRectangle[]			gameOver;
	
	ArrayList<GameElementRectangle>	listElementDump		= new ArrayList<GameElementRectangle>();
	DisplayRectangle				displayDump;
	public GameRectangle[]			dump;
	
	public GameRectangle			recMap;
	
	// ArrayList<GameElementSpriteBatch> listElementGroundTL = new
	// ArrayList<GameElementSpriteBatch>();
	// DisplaySpriteBatch displayGroundTL;
	// SpriteBatch batchGroundTL;
	// public GameSprite[] groundTL;
	//
	// ArrayList<GameElementSpriteBatch> listElementGroundTC = new
	// ArrayList<GameElementSpriteBatch>();
	// DisplaySpriteBatch displayGroundTC;
	// SpriteBatch batchGroundTC;
	// public GameSprite[] groundTC;
	//
	// ArrayList<GameElementSpriteBatch> listElementGroundTR = new
	// ArrayList<GameElementSpriteBatch>();
	// DisplaySpriteBatch displayGroundTR;
	// SpriteBatch batchGroundTR;
	// public GameSprite[] groundTR;
	//
	// ArrayList<GameElementSpriteBatch> listElementGroundCL = new
	// ArrayList<GameElementSpriteBatch>();
	// DisplaySpriteBatch displayGroundCL;
	// SpriteBatch batchGroundCL;
	// public GameSprite[] groundCL;
	//
	// ArrayList<GameElementSpriteBatch> listElementGroundC = new
	// ArrayList<GameElementSpriteBatch>();
	// DisplaySpriteBatch displayGroundC;
	// SpriteBatch batchGroundC;
	// public GameSprite[] groundC;
	//
	// ArrayList<GameElementSpriteBatch> listElementGroundCR = new
	// ArrayList<GameElementSpriteBatch>();
	// DisplaySpriteBatch displayGroundCR;
	// SpriteBatch batchGroundCR;
	// public GameSprite[] groundCR;
	//
	// ArrayList<GameElementSpriteBatch> listElementDump = new
	// ArrayList<GameElementSpriteBatch>();
	// DisplaySpriteBatch displayDump;
	// SpriteBatch batchDump;
	// public GameSprite[] dump;
	
	public Map(GameEngine engine, DisplayTmx tmx, MC mc, int xMove)
	{
		this.engine = engine;
		this.map = tmx.tiledMap[0];
		this.tmxLayer = tmx.tmxLayer[0];
		this.mc = mc;
		this.xMove = xMove;
		xCountMove = -xMove;
		
		loadObjectMap();
	}
	
	private void loadObjectMap()
	{
		if (GameEngineConfiguration.enable_trace)
			alpha = 0.4f;
		else
			alpha = 0;
		
		for (TMXObjectGroup group : map.getTMXObjectGroups())
		{
			if (group.getName().equals("P1")) for (TMXObject object : group.getTMXObjects())
				if (mc != null) mc.startPosition(object.getX(), object.getY());
			
			if (group.getName().equals("gameOver"))
				for (TMXObject object : group.getTMXObjects())
					listElementGameOver.add(new GameElementRectangle(object.getX(), object.getY(), object.getWidth(), object.getHeight(), Color.WHITE, 0,
						Anchor.TOP_LEFT, Ratio.NONE));
			
			if (group.getName().equals("ground"))
				for (TMXObject object : group.getTMXObjects())
					listElementGround.add(new GameElementRectangle(object.getX(), object.getY(), object.getWidth(), object.getHeight(), Color.WHITE, alpha,
						Anchor.TOP_LEFT, Ratio.NONE));
			
			if (group.getName().equals("recMap"))
				for (TMXObject object : group.getTMXObjects())
					recMap =
						new DisplayRectangle(engine, new GameElementRectangle(object.getX(), object.getY(), object.getWidth(), object.getHeight(), Color.WHITE,
							0, Anchor.TOP_LEFT, Ratio.NONE)).newRectangle(0);
			
			if (group.getName().equals("dump"))
				for (TMXObject object : group.getTMXObjects())
					listElementDump.add(new GameElementRectangle(object.getX(), object.getY(), object.getWidth(), object.getHeight(), Color.WHITE, alpha,
						Anchor.TOP_LEFT, Ratio.NONE));
		}
		
		displayGround = new DisplayRectangle(engine, listElementGround);
		ground = new GameRectangle[listElementGround.size()];
		for (int i = 0; i < listElementGround.size(); i++)
			ground[i] = displayGround.newRectangle(i);
		
		displayGameOver = new DisplayRectangle(engine, listElementGameOver);
		gameOver = new GameRectangle[listElementGameOver.size()];
		for (int i = 0; i < listElementGameOver.size(); i++)
			gameOver[i] = displayGameOver.newRectangle(i);
		
		displayDump = new DisplayRectangle(engine, listElementDump);
		dump = new GameRectangle[listElementDump.size()];
		for (int i = 0; i < listElementDump.size(); i++)
			dump[i] = displayDump.newRectangle(i);
		
		setX(xMove);
	}
	
	public void onUpdate()
	{	
		
	}
	
	public void setX(int x)
	{
		xCountMove += x;
		
		for (int i = 0; i < tmxLayer.length; i++)
		{
			tmxLayer[i].setX(tmxLayer[i].getX() + x);
		}
		
		recMap.setX(recMap.getX() + x);
		
		for (int i = 0; i < listElementGround.size(); i++)
		{
			ground[i].setX(ground[i].getX() + x);
		}
		
		for (int i = 0; i < listElementGameOver.size(); i++)
		{
			gameOver[i].setX(gameOver[i].getX() + x);
		}
		
		for (int i = 0; i < listElementDump.size(); i++)
		{
			dump[i].setX(dump[i].getX() + x);
		}
	}
	
	public void reset()
	{
		setX(-xCountMove);
		xCountMove = 0;
	}
}
